package com.gm.service.user;

import com.gm.dao.right.IRightDao;
import com.gm.dao.role.IRoleDao;
import com.gm.model.po.Right;
import com.gm.model.po.Role;
import com.gm.tools.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/6/13.
 */
@Service
public class RightService {
    @Autowired
    private IRightDao rightDao;

    @Autowired
    private IRoleDao roleDao;

    /**
     * 获取角色菜单权限
     * @param roles
     * @return
     */
    public List<Right> getRoleAuthority(String roles) {
        Integer role_id = Integer.valueOf(roles);
        Role role = roleDao.selectByPrimaryKey(role_id);
        String rights[] = role.getRights().split(",");
        List<Right> rightList = rightDao.getRightListByIds(rights);
        Iterator<Right> rightIterator = rightList.iterator();
        List<Right> parentRightList = new ArrayList<>();
        while (rightIterator.hasNext()) {
            Right right = rightIterator.next();
            if (right.getParentNode() == 0) {
                parentRightList.add(right);
                rightIterator.remove();
            }

        }
        Iterator<Right> sonRightIterator = rightList.iterator();
        while (sonRightIterator.hasNext()) {
            Right sonright = sonRightIterator.next();
            for (Right parentRight : parentRightList) {
                if (sonright.getParentNode() == parentRight.getId()) {
                    if(parentRight.getSonRight()==null){
                        parentRight.setSonRight(new ArrayList<Right>());
                    }
                    parentRight.getSonRight().add(sonright);
                }
            }
            sonRightIterator.remove();
        }

        return parentRightList;
    }

    /**
     * 获取所有菜单
     * @return
     */
    public List<Right> getALlRightList(){
        List<Right> parentRightList = rightDao.getRightListByLevel(0);
        for(Right right :parentRightList){
            List<Right> sonRightList = rightDao.getRightListByLevel(right.getId());
            right.setSonRight(sonRightList);
        }
        return parentRightList;
    }

    public List<Right> getRightList() {
        List<Right> parentRightList = rightDao.getRightListByLevel(0);
        return parentRightList;
    }

    public Right selectByPrimaryKey(Integer id){
        return rightDao.selectByPrimaryKey(id);
    }

    public int updateRight(Right right){
        Right r = rightDao.selectByPrimaryKey(right.getId());
        r.setName(right.getName());
        r.setUrl(right.getUrl());
        r.setUpdateTime(DateUtils.getCurrentUnixTime());
        return rightDao.updateByPrimaryKey(r);
    }

    public int addRight(Right right){
        right.setCreateTime(DateUtils.getCurrentUnixTime());
        right.setUpdateTime(DateUtils.getCurrentUnixTime());
        return rightDao.insert(right);
    }

    public int deleteRight(Integer rightId){
        return rightDao.deleteByPrimaryKey(rightId);
    }

}
